<?php
namespace app\controller;

use app\BaseController;
use app\common\controller\AdminController;
use app\common\model\system\Node;
use app\common\model\system\RoleNode;
use app\common\model\system\User;
use app\common\service\AuthService;
use app\common\service\system\NodeService;
use app\common\service\system\RoleService;

class Index extends AdminController
{
    protected $middleware = [];
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function test(){
        $res = app(NodeService::class)->getPermissionMenu(49);
        return success($res);
    }

    public function testTree(){
        $list = [
            [
                'id'=>1,
                'pid'=>0
            ],
            [
                'id'=>2,
                'pid'=>0
            ],
            [
                'id'=>3,
                'pid'=>1
            ],
        ];
        return json($this->getTree($list,0));
    }
    /**
     * @param array $arr
     * @param int $pid
     * @return array
     */
    public function getTree($arr,$pid=0,$parentField='pid'){
        $list =array();
        foreach ($arr as $k=>$v){
            if ($v[$parentField] == $pid){
                $v['children'] = $this->getTree($arr,$v['id'],$parentField);
                if(!$v['children']){
                    unset($v['children']);
                }
                $list[] = $v;
            }
        }
        return $list;
    }

    public function auth(){

//        $res = app(RoleNode::class)->getList(['role_id','in','6,7']);
//        $res = array_column($res,'node_id');
//
//        $nodes = app(Node::class)->getList(['id','in',$res]);
        $nodes = app(Node::class)->getTree();
        return success($nodes);
    }
    public function testModel(){
        $data = [
            'username' => 'test'.time()
        ];
        $model = new User();
        return $model->test();
        //$model->insertGetId($data);
    }
}
